package jsp;

import entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author:石小俊
 * @Date:2022/12/14 9:41
 * @Version:1.0-SNAPSHOT
 * @Description:
 */
@WebServlet("*.session")
public class SessionServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/add.session".equals(path)){
            addSession(request,response);
        }
        if("/find.session".equals(path)){
            findSession(request,response);
        }
        if("/modify.session".equals(path)){
            modifySession(request,response);
        }
        if("/remove.session".equals(path)){
            removeSession(request,response);
        }
        if("/clean.session".equals(path)){
            cleanSession(request,response);
        }
        if("/setMaxAge.session".equals(path)){
            setMaxAge(request,response);
        }
    }

    public void setMaxAge(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Session的默认生存时间为:会话结束或者30分钟
        // Session的生存时间一般不能用于实现删除
        // 设置生存时间为0表示永不过期
        // 设置1秒,表示1秒后session自动销毁
        // 参数单位:秒
        // 开发中不建议设置
        request.getSession().setMaxInactiveInterval(1);
    }
    public void removeSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // session提供了两种删除方法
        // 1.删除指定key的数据
        request.getSession().removeAttribute("msg");
    }
    public void cleanSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 2.清空整个session
        // invalidate()实现机制
        // 首先删除当前的Session对象
        // 创建一个新的Session
        request.getSession().invalidate();
    }
    public void modifySession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // session没有提供修改方法
        // 但是存储数据的时候,遇到相同的key时会覆盖
        request.getSession().setAttribute("msg","我是新的数据");
    }
    public void findSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String msg = (String) session.getAttribute("msg");
        Emp emp = (Emp) session.getAttribute("emp");
        System.out.println(msg);
        System.out.println(emp);
    }
    public void addSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取session对象
//        HttpSession session = request.getSession(false);
//        HttpSession session2 = request.getSession(true);
        HttpSession session = request.getSession();
        // 向session作用域中存储数据
        session.setAttribute("msg","Hello Session");
        Emp emp = new Emp();
        emp.setId(1);
        emp.setName("admin");
        session.setAttribute("emp",emp);
    }

}
